
import { createVNode ,render} from 'vue'
import coms from "./coms";

const body = document.body;
const root = document.createElement("div");
body.appendChild(root);
root.className = "custom-root";
root.id = "modal-container";

let cache = {};

function initCache(key,com,app){
    if(cache[key]){
        return cache[key];
    }
    let div = document.createElement("div");
    root.appendChild(div);
    let vm = createVNode(com,{},{
        //
       // default:()=>createVNode(slot)
    });
    vm.appContext = app._context;
    render(vm,div);
    return vm;
}

function OpenCom(key,com,app){
    return (opts)=>{
        let instance = initCache(key,com,app);
        cache[key] = instance;
        instance.component.proxy.open && instance.component.proxy.open(opts);
        let p = new Promise((resolve,reject)=>{
            instance.component.proxy._promise = {resolve,reject}
        });
        return p;
    }
}


export default {
    install(app){
        for(let key in coms){
           app.config.globalProperties[`$${key}`] = OpenCom(key,coms[key],app);
        }
    }
}